import pandas as pd
import json
import csv


def main():
    # 读取 Excel 文件中的所有工作表
    file_path = 'NSK深沟球轴承数据表.xlsx'
    xls = pd.read_excel(file_path, sheet_name=None)

    # 创建一个列表来存储问题和JSON数据
    data_list = []

    # 遍历每个工作表
    for sheet_name, df in xls.items():
        print(f"Processing sheet: {sheet_name}")

        # 遍历DataFrame的每一行
        for index, row in df.iterrows():
            # 提取型号名称
            model_name = row['型号']

            # 将整行数据转换为字典
            row_dict = row.to_dict()

            # 将字典转换为JSON字符串
            row_json = json.dumps(row_dict, ensure_ascii=False)

            # 生成问题
            question = f" {model_name} "

            # 将问题和JSON数据添加到列表中
            data_list.append([question, row_json])

    # 写入CSV文件
    output_file = 'NSK深沟球轴承数据表.csv'
    with open(output_file, mode='w', newline='', encoding='utf-8') as file:
        writer = csv.writer(file)
        writer.writerow(['问题', '参数'])
        for data in data_list:
            writer.writerow(data)

    print(f"数据已成功写入 {output_file}")


if __name__ == "__main__":
    main()
